package edu.umn.contactviewer;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import edu.umn.contactviewer.models.Contact;
import edu.umn.contactviewer.webservice.WebServiceClient;
import android.util.Log;

/**
 * Created by IntelliJ IDEA.
 * User: Socer3d
 * Date: 2/25/12
 * Time: 6:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class ContactEditActivity extends Activity implements View.OnClickListener {
    private Contact contact;
    EditText nameField;
    EditText titleField;
    EditText phoneField;
    EditText emailField;
    EditText twitterField;
    //Contact newContact;
    
    class AddContactTask extends AsyncTask<Contact, Contact, Contact> {

    	@Override
    	protected Contact doInBackground(Contact... contact1) {
    		return WebServiceClient.addContact(contact1[0]);
    	}
    	
    	protected void onPostExecute(Contact contact2) {
    		super.onPostExecute(contact2);
    		//newContact = contact2;
    		
    		presentInfoView(contact2);
    	}
    }

    class EditContactTask extends AsyncTask<Contact, Contact, Contact> {
        Contact editedContact;
        @Override
        protected Contact doInBackground(Contact... contact1) {
            editedContact = contact1[0];
            return WebServiceClient.updateContact(contact1[0]);
        }

        protected void onPostExecute(Contact contact2) {
            super.onPostExecute(contact2);
            //newContact = contact2;

            /**
             * The web service has a bug where it returns the old contact
             * information instead of the new contact information on a
             * successful update, so for now just serialize the incoming
             * contact data
             */
            //presentInfoView(contact2);
            presentInfoView(editedContact);
        }
    }

    private void presentInfoView(Contact newContact) {
    	//go back to Info view
        Bundle bundle = new Bundle();
        bundle.putSerializable("contact", newContact);
        Intent intent = new Intent(this, ContactInfoActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_edit);
        Bundle b = this.getIntent().getExtras();
        contact = (Contact) b.getSerializable("contact");
        ToolbarConfig toolbarConfig = null;
        if(contact.get_id() == null  || contact.get_id().isEmpty()) {
            toolbarConfig = new ToolbarConfig(this, "Add Contact");
        }
        else {
            toolbarConfig = new ToolbarConfig(this, "Edit Contact");
        }
        toolbarConfig.getToolbarBackButton().setText( "Cancel" );
        toolbarConfig.getToolbarRightButton().setText( "Save" );

        nameField = ((EditText)findViewById(R.id.contact_name));
        nameField.setText(this.contact.getName());
        titleField = ((EditText)findViewById(R.id.contact_title));
        titleField.setText(contact.getTitle());
        ((TextView)findViewById(R.id.twitter)).setText("Twitter ID:");
        twitterField = ((EditText)findViewById(R.id.contact_twitter));
        twitterField.setText(contact.getTwitterId());
        ((TextView)findViewById(R.id.email)).setText("Email:");
        emailField = ((EditText)findViewById(R.id.contact_email));
        emailField.setText(contact.getEmail());
        ((TextView)findViewById(R.id.phone)).setText("Phone:");
        phoneField = ((EditText)findViewById(R.id.contact_phone));
        phoneField.setText(contact.getPhone());

        Log.i("ContactEditActivity", "contact id = " + contact.get_id());

        findViewById(R.id.toolbar_right_button).setOnClickListener(this);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.toolbar_right_button) {
            //get values
        	contact.setName(nameField.getText().toString());
            contact.setTitle(titleField.getText().toString());
            contact.setEmail(emailField.getText().toString());
            contact.setPhone(phoneField.getText().toString());
            contact.setTwitterId(twitterField.getText().toString());

            if(contact.get_id() == null  || contact.get_id().isEmpty()) {
                Log.i("ContactEditActivity", "creating a new contact");
                new AddContactTask().execute(contact);
            }
            else {
                Log.i("ContactEditActivity", "editing an existing contact");
                new EditContactTask().execute(contact);
            }
        }
    }
}